package com.hyt.it.ogt.kq.main.controller.bm;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.utils.exception.BusinessException;
import com.hyt.common.utils.result.Result;
import com.hyt.core.enums.DelFlagEnum;
import com.hyt.core.util.DateUtils;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.cj.model.vo.OptionTree;
import com.hyt.it.ogt.cj.model.vo.ProjectCollectInfoVo;
import com.hyt.it.ogt.cj.service.IProjectCollectInfoService;
import com.hyt.it.ogt.kq.common.bm.enums.BmProjectStateEnum;
import com.hyt.it.ogt.kq.common.bm.swagger.BmApiVersionConstant;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.feign.tps.TpsClient;
import com.hyt.it.ogt.kq.service.bm.feign.tps.model.Bucket;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectExam;
import com.hyt.it.ogt.kq.service.bm.model.entity.StudentSubject;
import com.hyt.it.ogt.kq.service.bm.model.vo.*;
import com.hyt.it.ogt.kq.service.bm.service.*;
import com.hyt.log.annotation.Log;
import com.hyt.loginfo.model.Tree;
import com.hyt.loginfo.model.User;
import com.hyt.loginfo.remote.PermissionLoginClient;
import com.hyt.loginfo.service.IBaseLoginService;
import com.hyt.loginfo.service.IUaUserExtService;
import com.hyt.loginfo.util.OfficeIdToNameUtil;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 报名项目 前端控制器
 * </p>
 *
 * @author wangt
 * @since 2020-05-15
 */
@RestController
@Api(tags = "5.报名项目", value = "报名项目")
@ApiSort(value = 5)
@Slf4j
@RequestMapping("/bm/project")
public class ProjectController extends BaseController {

    @Resource
    OfficeIdToNameUtil officeIdToNameUtil;
    @Resource
    private IProjectService iProjectService;
    @Resource
    TpsClient tpsClient;
    @Resource
    private IProjectCollectInfoService iProjectCollectInfoService;
    @Resource
    private IStudentInfoService iStudentInfoService;
    @Resource
    private IStudentSubjectService iStudentSubjectService;
    @Resource
    IProjectExamService iProjectExamService;
    @Resource
    private PermissionLoginClient permissionLoginClient;
    @Resource
    IUserPermissionService iUserPermissionService;
    @Resource
    private IStudentProjectService iStudentProjectService;
    @Resource
    IUaUserExtService iUaUserExtService;
    @Resource
    IBaseLoginService iBaseLoginService;

    @ApiOperation(value = "5.1.1 添加或编辑项目", notes = "20000:成功；13010101：添加项目数据失败,13010107:开始时间必须小于结束时间;")
    @ApiOperationSort(value = 1)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31051114, print = "操作报名任务失败")
    @Log(module = "报名项目管理", business = "添加或编辑项目")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResponse<Project> save(@RequestBody Project project) {
        log.debug("[发布取消发布报名]请求参数：{}", FastJsonUtil.getBeanToJson(project));
        return iProjectService.publishProject(project);
    }

    @ApiOperation(value = "5.1.2 查看项目数据", notes = "20000:成功；31051201：未查找到项目数据")
    @ApiOperationSort(value = 2)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31051201, print = "查看项目数据异常")
    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET)
    public ApiResponse<ProjectVo> query(@PathVariable String id) {
        ProjectVo projectVo = iProjectService.queryProjectById(id);
        return ApiResponse.<ProjectVo>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(projectVo)
                .build();
    }

    @ApiOperation(value = "5.1.3 查询分页数据",
            notes = "20000:成功,31010132:查询失败，异常",
            response = ProjectStatVo.class)
    @ApiOperationSort(value = 3)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31051301, print = "查询分页数据失败")
    @GetMapping(value = "/queryPage")
    public ApiResponse<Page<ProjectStatVo>> queryPage(ProjectParam projectParam) {
        String deptId = getDeptId();
        Set<String> deptIds = iBaseLoginService.getDeptIds();
        String userId = getUserId();
        String officeId = getOfficeId();
        if (StringUtils.isNotBlank(projectParam.getProjectState())) {
            projectParam.setCurrentTime(DateUtils.getNowTime("yyyy-MM-dd HH:mm:ss"));
        }
        if(StrUtil.isEmptyIfStr(userId) || StrUtil.isEmptyIfStr(officeId)) {
		    return ApiResponse.<Page<ProjectStatVo>>builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
		}
		projectParam.setOfficeId(officeId);
		projectParam.setDeptId(deptId);
		projectParam.setDeptIds(deptIds);
        projectParam.setUserId(userId);

        Page<ProjectStatVo> page = iProjectService.queryPage(projectParam);
        return ApiResponse.<Page<ProjectStatVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(page)
                .build();
    }

    @ApiOperation(value = "5.1.6 预览项目数据", notes = "20000:成功；31010135：未查找到项目数据；31010136：查询异常")
    @ApiOperationSort(value = 6)
    @ThrowsAdvice(code = 31050000, print = "预览项目数据异常")
    @RequestMapping(value = "/previewProject/{projectId}", method = RequestMethod.GET)
    public Object previewProject(@PathVariable String projectId) throws Exception {
        ProjectPreviewVo vo = null;
        vo = iProjectService.previewProject(projectId);
        if (vo != null) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(vo).build();
        } else {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051601).data(vo).build();
        }
    }

    @ApiOperation(value = "5.1.7 删除项目数据", notes = "20000:成功；14025：删除异常")
    @ApiOperationSort(value = 7)
    @Log(module = "报名项目管理", business = "删除项目数据")
    @ThrowsAdvice(code = 31051703, print = "删除项目数据异常")
    @RequestMapping(value = "/deleteProject", method = RequestMethod.DELETE)
    @ApiVersion(group = BmApiVersionConstant.V2_4_p18)
    public ApiResponse<Object> deleteProject(String projectId) throws Exception {
        User user = getUser();
        if(null == user || StrUtil.isEmptyIfStr(user.getId())) {
            return ApiResponse.builder().code(31051707).build();
        }
        if(StrUtil.isBlank(projectId)) {
            return ApiResponse.builder().code(31051704).build();
        }
        // 查询项目信息
        Project project = iProjectService.getById(projectId);
        if(null == project) {
            return ApiResponse.builder().code(31051705).build();
        }
        // 项目已发布状态下不允许删除
        Integer publishState = project.getStatus();
        if(null != publishState && publishState.equals(BmProjectStateEnum.PUBLISHED.getState())) {
            return ApiResponse.builder().code(31051706).build();
        }
        // 报名任务下已有报名考生，不允许删除
        Integer studentSubjectCount = iStudentSubjectService.lambdaQuery()
                .eq(StudentSubject::getProjectId, projectId)
                .eq(StudentSubject::getDelFlag, DelFlagEnum.NORMAL.getCode())
                .count();
        if (studentSubjectCount > 0) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051702).build();
        } 
        // 报名任务被考务绑定，不允许删除
        Integer projectExamListCount = iProjectExamService.lambdaQuery()
                .eq(ProjectExam::getProjectId, projectId)
                .eq(ProjectExam::getDelFlag, DelFlagEnum.NORMAL.getCode())
                .count();
        if (projectExamListCount > 0) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051701).build();
        }
        
        project.setDelFlag(Boolean.TRUE);
        project.setUpdateBy(user.getId());
        project.setUpdateDate(LocalDateTime.now());
        Boolean updateResult = iProjectService.updateById(project);
        //iProjectService.deleteProject(projectId);
        if(updateResult) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
        }else {
            return ApiResponse.builder().code(31051703).build();
        }
    }

    @ApiOperation(value = "5.1.8 统计项目数据", notes = "20000:成功", response = Map.class)
    @ApiOperationSort(value = 8)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31051801, print = "统计项目数据查询异常")
    @GetMapping(value = "/statProjectStudent")
    public ApiResponse<Map<String, Object>> statProjectStudent(String projectId) {
        ProjectStatVo resultVo = iProjectService.statProjectStudent(projectId);
        Map<String, Object> map = new HashMap<>();
        map.put("result", resultVo);
        return ApiResponse.<Map<String, Object>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(map)
                .build();
    }

    @ApiOperation(value = "5.1.9 上传项目图片", notes = "20000:成功;14041:文件格式不正确;14042:文件为空;14043:上传文件异常")
    @ApiOperationSort(value = 9)
    @ThrowsAdvice(code = 31051904, print = "上传项目图片失败")
    @Log(module = "报名项目管理", business = "上传项目图片")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ApiResponse<String> uploadFile(MultipartFile file) {
        String fileSysPath = iProjectService.uploadFile(file);
        return ApiResponse.<String>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(fileSysPath)
                .build();
    }

    @ApiOperation(value = "5.1.11 查询考生报名项目", notes = "20000:成功;31010136:查询异常", response = ProjectStatVo.class, responseContainer = "List")
    @ApiOperationSort(value = 11)
    @ThrowsAdvice(code = 31050000, print = "查询考生报名项目异常")
    @RequestMapping(value = "/queryStudentProject", method = RequestMethod.GET)
    public ApiResponse queryStudentProject(String officeId, String regId) throws Exception {
        if (StringUtils.isBlank(officeId)) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051111).build();
        }
        List<ProjectStatVo> list = new ArrayList<>();
        if (StringUtils.isEmpty(regId)) {
            list = iProjectService.queryStudentProjecting(officeId, regId);
        } else {
            list = iProjectService.queryStudentProject(officeId, regId);
        }
        if (CollectionUtils.isNotEmpty(list)) {
            iStudentProjectService.getEffectivePeoject(list, StringUtils.isEmpty(regId) ? getUserId() : regId);
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
    }

    @ApiOperation(value = "5.1.11 当前报名", notes = "20000:成功;31010136:查询异常", response = ProjectStatVo.class, responseContainer = "List")
    @ApiOperationSort(value = 11)
    @ThrowsAdvice(code = 31050000, print = "当前报名异常")
    @RequestMapping(value = "/queryEffectiveProject", method = RequestMethod.GET)
    public ApiResponse<List<ProjectStatVo>> queryEffectiveProject(String officeId, String regId)  {
		if (StringUtils.isEmpty(regId)) {
			regId = getUserId();
		}
		if (StringUtils.isBlank(officeId)) {
            officeId = getOfficeId();
        }
		if (StringUtils.isBlank(officeId)) {
			return ApiResponse.<List<ProjectStatVo>>builder()
                    .code(Constant.RESPONSE_CODE_31051111)
                    .build();
        }
        List<ProjectStatVo> newList = iProjectService.queryEffectiveProject(officeId, regId);
        return ApiResponse.<List<ProjectStatVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(newList)
                .build();
    }

    @ApiOperation(value = "5.1.12 工作台统计报名数", notes = "20000:成功;31010136:查询异常")
    @ApiOperationSort(value = 12)
    @ThrowsAdvice(code = 31050000, print = "工作台统计报名数异常")
    @RequestMapping(value = "/statStudent", method = RequestMethod.GET)
    public Object statStudent(ProjectParam param) throws Exception {
        param.setOfficeId(getOfficeId());
        param.setUserId(getUserId());
        param.setDeptIds(iBaseLoginService.getDeptIds());
        FirstStatVo vo = iProjectService.statStudent(param);
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(vo)
                .build();
    }

    /**
     * 根据名称查询机构
     */
    @ApiOperation(value = "根据名称查询机构", notes = "根据名称查询机构")
    @RequestMapping(value = "/queryOffice", method = RequestMethod.GET)
    public Object queryOffice(String officeName) throws BusinessException {
        List<Tree> list = officeIdToNameUtil.fuzzySearchTreeByName(officeName);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setId(list.get(i).getId() + "@" + list.get(i).getText());
            }
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
    }

    @ApiOperation(value = "5.1.13 根据项目id查询报名流程配置", notes = "20000:成功")
    @ApiOperationSort(value = 13)
    @ThrowsAdvice(code = 31050000, print = "根据项目id查询报名流程配置异常")
    @RequestMapping(value = "/queryNodeList", method = RequestMethod.GET)
    public ApiResponse<List<ProjectNodeVo>> queryNodeList(String projectId) {
        List<ProjectNodeVo> list = iProjectService.queryNodeList(projectId);
        return ApiResponse.<List<ProjectNodeVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(list)
                .build();
    }

    @ApiOperation(value = "5.1.15 根据项目id修改流程配置", notes = "20000:成功")
    @ApiOperationSort(value = 13)
    @ThrowsAdvice(code = 31051151, print = "根据项目id修改流程配置异常")
    @Log(module = "报名项目管理", business = "根据项目id修改流程配置")
    @RequestMapping(value = "/updateNodeList", method = RequestMethod.POST)
    public ApiResponse<List<ProjectNodeVo>> updateNodeList(@RequestBody List<ProjectNodeVo> list) {
        iProjectService.updateNodeList(list);
        return ApiResponse.<List<ProjectNodeVo>>builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
    }

    @ApiOperation(value = "5.1.14 查看报名项目采集信息数据", notes = "20000:成功；14029：未查找到报名项目采集信息数据")
    @ApiOperationSort(value = 14)
    @ThrowsAdvice(code = 31051141, print = "查看报名项目采集信息数据异常")
    @RequestMapping(value = "/queryByProjectId/{projectId}", method = RequestMethod.GET)
    public ApiResponse<List<ProjectCollectInfoVo>> queryByProjectId(@PathVariable String projectId) throws Exception {
        List<ProjectCollectInfoVo> list = iProjectCollectInfoService.queryByProjectId(projectId);
        ApiResponse api = permissionLoginClient.getDeptTree();
        List<Tree> treeList = (List<Tree>) api.getData();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTitle().equals("组织机构")) {
                List<OptionTree> treeNodes = new ArrayList<OptionTree>();
                treeNodes = this.praseTree(treeNodes, treeList);
                treeNodes = bulidByLoop(treeNodes);
                list.get(i).setOptionJson(treeNodes);
            }
        }
        return ApiResponse.<List<ProjectCollectInfoVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(list)
                .build();
    }

    /**
     * 双层循环实现建树,对传入的树节点列表无排序要求
     *
     * @param treeNodes 传入的树节点列表
     * @return
     */
    public List<OptionTree> bulidByLoop(List<OptionTree> treeNodes) {
        List<OptionTree> trees = new ArrayList<OptionTree>();
        for (OptionTree treeNode : treeNodes) {
            /*
             * 根节点的父Id为0,且不要求仅有唯一的根
             */
            if (StringUtils.isBlank(treeNode.getParentValue())) {
                trees.add(treeNode);
            }
            for (OptionTree it : treeNodes) {
                if (treeNode.getValue().equals(it.getParentValue())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<OptionTree>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return trees;
    }

    public List<OptionTree> praseTree(List<OptionTree> tree, List<Tree> treeList) {
        for (int i = 0; i < treeList.size(); i++) {
            OptionTree optionTree = new OptionTree();
            optionTree.setLabel(treeList.get(i).getText());
            optionTree.setValue(treeList.get(i).getId());
            optionTree.setParentValue(treeList.get(i).getPid());
            tree.add(optionTree);
            if (treeList.get(i).getChildren() != null && treeList.get(i).getChildren().size() > 0) {
                praseTree(tree, treeList.get(i).getChildren());
            }
        }
        return tree;
    }

    @ApiOperation(value = "5.1.15 查询所有项目", notes = "20000:成功,31010136:查询失败，异常")
    @ApiOperationSort(value = 15)
    @ThrowsAdvice(code = 31050000, print = "查询所有项目异常")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ApiResponse<List<Project>> list() {
        QueryWrapper<Project> wrapper = new QueryWrapper<Project>();
        wrapper.eq("office_id", getOfficeId());
        List<Project> list = iProjectService.list(wrapper);
        return ApiResponse.<List<Project>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(list)
                .build();
    }

    @ApiOperation(value = "5.1.15 复制项目", notes = "20000:成功,31010136:查询失败，异常")
    @ApiOperationSort(value = 15)
    @Log(module = "报名项目管理", business = "复制项目")
    @RequestMapping(value = "/copyProject", method = RequestMethod.POST)
    @ApiVersion(group = BmApiVersionConstant.V2_4_p18)
    @ThrowsAdvice(code = 31511505, print = "复制项目异常")
    public ApiResponse<Boolean> copyProject(@RequestBody ProjectCopyVo projectCopyVo) throws Exception {
        User user = getUser();
        if(null == user || StrUtil.isEmptyIfStr(user.getId())) {
            return ApiResponse.<Boolean>builder().code(31051707).data(Boolean.FALSE).build();
        }
        // 校验项目ID是不是存在
        String projectId = projectCopyVo.getOldProjectId();
        if(StrUtil.isEmptyIfStr(projectId)) {
            return ApiResponse.<Boolean>builder().code(31511501).data(Boolean.FALSE).build();
        }
        Project project = iProjectService.getById(projectId);
        if(null == project) {
            return ApiResponse.<Boolean>builder().code(31511502).data(Boolean.FALSE).build();
        }
        String projectName = projectCopyVo.getNewProjectName();
        if(StrUtil.isEmptyIfStr(projectName)) {
            return ApiResponse.<Boolean>builder().code(31511503).data(Boolean.FALSE).build();
        } 
        Boolean projectNameExits = iProjectService.checkProjectNameExits(projectName, getOfficeId());
        if(StrUtil.isEmptyIfStr(projectNameExits)) {
            return ApiResponse.<Boolean>builder().code(31511504).data(Boolean.FALSE).build();
        }
        Boolean result = iProjectService.copyProject(projectCopyVo);
        return ApiResponse.<Boolean>builder().code(Constant.RESPONSE_CODE_SUCCESS).data(result).build();
    }

    @ApiOperation(value = "5.1.16 批量修改报名项目采集信息", notes = "20000:成功；31010138：新增修改采集信息数据 失败;31010137:修改采集项为空")
    @ApiOperationSort(value = 16)
    @Log(module = "报名项目管理", business = "批量修改报名项目采集信息")
    @RequestMapping(value = "/updateProjectCollect", method = RequestMethod.POST)
    public Object updateProjectCollect(@RequestBody List<ProjectCollectInfoVo> list) {
        if (list != null && list.size() > 0) {
            return iProjectService.updateProjectCollect(list);
        } else {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051161).data("采集项为空").build();
        }
    }

    @ApiOperation(value = "5.1.17 获取bucket临时凭证", response = BucketVo.class)
    @ApiOperationSort(17)
    @RequestMapping(value = "/getBucketTempCredential", method = RequestMethod.GET)
    public ApiResponse<BucketVo> getBucketTempCredential(@RequestParam String projectId,
                                                         @RequestParam(required = false) String projectCollectInfoId) {
        BucketVo res = new BucketVo();
        try {
            String json = tpsClient.getBucketTempCredential("0");
            Result result = JSON.parseObject(json, Result.class);
            if (result.getObj() != null) {
                Bucket bucket = JSON.parseObject(result.getObj().toString(), Bucket.class);
                BeanUtils.copyProperties(bucket, res);
                res.setDomain(res.getDomain() + "/");
                if (StringUtils.isBlank(projectCollectInfoId)) {
                    // 腾讯云
                    res.setPath("bmPhoto/" + projectId + "/temp/");

                } else {
                    // 腾讯云
                    res.setPath("bmPhoto/" + projectId + "/" + projectCollectInfoId + "/");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResponse.<BucketVo>builder().code(Constant.RESPONSE_CODE_SUCCESS).data(res).build();
    }

    @ApiOperation(value = "5.1.18 通过虚拟officeId获取真实的OfficeId", notes = "20000:成功,31010136:查询失败，异常")
    @ApiOperationSort(value = 18)
    @ThrowsAdvice(code = 31050000, print = "通过虚拟officeId获取真实的OfficeId异常")
    @RequestMapping(value = "/getRealOfficeId", method = RequestMethod.GET)
    public Object getRealOfficeId(@RequestParam String officeId) {
        String realOfficeId = iProjectService.getRealOfficeId(officeId);
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(realOfficeId).build();
    }

	@ApiOperation(value = "5.1.20 小程序查询机构可报名的项目，包含用户正在报名的", notes = "20000:成功,14021:查询失败，异常", response = ProjectStatVo.class)
	@ApiOperationSort(value = 20)
	@ThrowsAdvice(code = 31050000, print = "小程序查询机构可报名的项目异常")
	@RequestMapping(value = "/mini/list", method = RequestMethod.GET)
	public ApiResponse<List<ProjectStatVo>> miniList(@RequestParam(required = false) String officeId, @RequestParam(required = false) String regId)  {
       if (StringUtils.isEmpty(regId)) {
            regId = getUserId();
        }
        if (StringUtils.isBlank(officeId)) {
            return ApiResponse.<List<ProjectStatVo>>builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
        }		
        List<ProjectStatVo> voList = iProjectService.getMiniList(officeId, regId);
        return ApiResponse.<List<ProjectStatVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(voList)
                .build();
    }

    @ApiOperation(value = "5.1.21 报名记录", notes = "20000:成功,14021:查询失败，异常", response = ProjectStatVo.class)
    @ApiOperationSort(value = 21)
    @ThrowsAdvice(code = 31050000, print = "小程序报名记录异常")
    @RequestMapping(value = "/mini/record", method = RequestMethod.GET)
    public ApiResponse<List<ProjectStatVo>> miniRecord(@RequestParam String officeId, @RequestParam String regId) {
        return ApiResponse.<List<ProjectStatVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProjectService.queryStudentProjected(officeId, regId))
                .build();
    }

    @ApiOperation(value = "5.1.22 文件上传华为云", notes = "fileType：poster-海报，qrcode-二维码, cj-采集信息")
    @ApiOperationSort(value = 22)
    @RequestMapping(value = "/uploadFileToHwCloud", method = RequestMethod.POST)
    public Object uploadFileToHwCloud(MultipartFile file, @RequestParam(required = false) String fileType) {
        if (null == file) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051904).build();
        }
        String fileName = file.getOriginalFilename();
        if (StringUtils.isEmpty(fileName)) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051904).build();
        }
        try {
            String url = iProjectService.uploadFileToHwCloud(file, fileType);
            if (StringUtils.isNotEmpty(url)) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(url).build();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_31051904).build();
    }

    @ApiOperation(value = "5.2.1 创建报名统计")
    @ApiOperationSort(value = 23)
    @ThrowsAdvice(code = 31191601, print = "创建报名统计异常")
    @GetMapping(value = "/createProjectStatistic")
    public void createProjectStatistic(@ApiParam("报名ID") @RequestParam(required = false) String projectId) {
        iProjectService.createProjectStatistic(projectId);
    }

    @ApiOperation(value = "5.3.1 查询当前机构下的所有报名项目", notes = "20000:成功," +
            "31053101：查询当前机构下的所有报名项目失败；" +
            "31053102：无效的机构"
    )
    @ApiOperationSort(value = 24)
    @ThrowsAdvice(code = 31053101, print = "查询当前机构下的所有报名项目异常")
    @GetMapping(value = "/simpleProjectList")
    @ApiVersion(group = BmApiVersionConstant.V2_4_p18)
    public ApiResponse<List<SimpleProjectVO>> simpleProjectList(String officeId, String keyword) {
        if(StrUtil.isEmptyIfStr(officeId)) {
            officeId = getOfficeId();
        }
        if (StrUtil.isBlank(officeId)) {
            return ApiResponse.<List<SimpleProjectVO>>builder()
                    .code(31053102)
                    .build();
        }
        return ApiResponse.<List<SimpleProjectVO>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProjectService.simpleProjectList(officeId))
                .build();
    }
}
